package com.silverhaze.diamond.client.data;

import com.silverhaze.commons.db.datasource.ObjectEntity;
import com.silverhaze.commons.utils.DateUtils;
import com.silverhaze.diamond.client.data.dao.DAOLocator;
import com.silverhaze.diamond.client.utils.ProjectUtils;
import com.silverhaze.diamond.client.utils.db.ServiceDataSource;
import com.silverhaze.diamond.common.entity.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * SystemUser: Babanin
 * Date: 13.07.2007
 * Time: 0:50:29
 */
public final class SpecificData {
    private SpecificData() {
    }

    public static List<Customer> filterCustomers(String name, String phone, String address) {
        List<Customer> result = new ArrayList<Customer>();

        final Pattern namePtr = ProjectUtils.buildPattern(name);
        final Pattern phonePtr = ProjectUtils.buildPattern(phone);
        final Pattern addressPtr = ProjectUtils.buildPattern(address);

        for (Customer c : ServiceDataSource.getInstance().getBeans(Customer.class)) {
            boolean match = true;
            match = (name == null) || namePtr.matcher(c.getExtendedName()).find();
            if (match) {
                match = (phone == null) || phonePtr.matcher(c.getPhone()).find();
                if (match) {
                    match = (address == null) || addressPtr.matcher(c.getAddress()).find();
                    if (match) {
                        result.add(c);
                    }
                }
            }
        }

        return result;
    }

    public static List<Customer> filterCustomers(String extendedName) {
        return filterCustomers(extendedName, "*", "*");

//        List<Customer> result = new ArrayList<Customer>();
//
//        final Pattern namePtr = ProjectUtils.buildPattern(extendedName);
//
//        for (Customer c : ServiceDataSource.getInstance().getBeans(Customer.class)) {
//            if (namePtr.matcher(c.getExtendedName()).matches()) {
//                result.add(c);
//            }
//        }
//
//        return result;
    }

    public static List filterDetails(String name, List<ObjectEntity> details) {
        List<Detail> result = new ArrayList<Detail>();

        final Pattern namePtr = ProjectUtils.buildPattern(name);
        for (ObjectEntity oe : details) {
            Detail detail = (Detail) oe;

            if (namePtr.matcher(detail.getName()).matches()) {
                result.add(detail);
            }
        }

        return result;
    }

    public static List filterServices(String owner, String device, String serialNumber, SystemUser user, Date fromDate, Date tillDate, boolean showComplete, boolean showInService, List<ObjectEntity> services) {
        List<Service> result = new ArrayList<Service>();

        final Pattern ownerPtr = ProjectUtils.buildPattern(owner);
        final Pattern devicePtr = ProjectUtils.buildPattern(device);
        final Pattern serialNumberPtr = ProjectUtils.buildPattern(serialNumber);

        for (ObjectEntity oe : services) {
            Service s = (Service) oe;

            if (user != null) {
                if (!user.equals(s.getSystemUser())) {
                    continue;
                }
            }

            if ((showComplete && s.isServiceComplete()) || (showInService && !s.isServiceComplete())) {
                if (DateUtils.between(fromDate, s.getDateProduce(), tillDate)) {
                    boolean match = true;

                    match = (owner == null) || ownerPtr.matcher(s.getOwner().getExtendedName()).find();
                    if (match) {
                        match = (device == null) || devicePtr.matcher(s.getTechNames()).find();
                        if (match) {
                            match = (serialNumber == null) || serialNumberPtr.matcher(s.getSerialNumber()).find();
                            if (match) {
                                result.add(s);
                            }
                        }
                    }
                }
            }
        }

        return result;
    }

    public static int getCompletedServiceCount() {
        int count = 0;

        for (ObjectEntity service : DAOLocator.getInstance().getServices()) {
            if (((Service) service).isServiceComplete()) {
                count++;
            }
        }

        return count;
    }

    public static int getAllServicesCount() {
        return DAOLocator.getInstance().getServices().size();
    }

    public static List<Service> getServicesBy(SystemUser user) {
        List<Service> result = new ArrayList<Service>();

        if (user != null) {
            for (ObjectEntity oe : DAOLocator.getInstance().getServices()) {
                Service s = (Service) oe;

                if (s.getSystemUser().equals(user)) {
                    result.add(s);
                }
            }

        }

        return result;
    }

    public static List<Service> getServicesBy(Detail detail) {
        List<Service> result = new ArrayList<Service>();

        if (detail != null) {
            for (ObjectEntity oe : DAOLocator.getInstance().getServices()) {
                Service s = (Service) oe;

                if (s.isServiceComplete()) {
                    boolean added = false;

                    for (BrokenDetail brokenDetail : s.getBrokenDetailes()) {
                        if (brokenDetail.getDetail().equals(detail)) {
                            result.add(s);
                            added = true;
                            break;
                        }
                    }

                    if (!added) {
                        for (UsedDetail usedDetail : s.getUsedDetailes()) {
                            if (usedDetail.getDetail().equals(detail)) {
                                result.add(s);
                                break;
                            }
                        }
                    }
                }
            }
        }

        return result;
    }

    public static List<Service> getServicesBy(Customer customer) {
        List<Service> result = new ArrayList<Service>();

        if (customer != null) {
            for (ObjectEntity oe : DAOLocator.getInstance().getServices()) {
                Service s = (Service) oe;

                if (s.getOwner().equals(customer)) {
                    result.add(s);
                }
            }
        }

        return result;
    }
}

